
//
//	cove.display.Graphics - 2011 May 18th
//
//	The Graphics class contains a set of methods that you can use to create a vector shape.
//
//-------------------------------------------------------------------------------------------------

var Graphics = cove.display.Graphics = Class.extend({
	
	init: function(displayObject) {
		this._displayObject = displayObject;
		this._svgElement = displayObject._svgElement;
		
		this._groupElement = xb.SVG.create('g');
		
		this._svgElement.appendChild(this._groupElement);
	},
	
	_redraw: function() {
		if(!this._displayObject._stage)
			__covePool__.addChild(this._displayObject);
		
		var groupBox = this._groupElement.getBBox();
		
		this._groupElement.setAttribute('transform', 'translate(' + (-groupBox.x) + ',' + (-groupBox.y) + ')');
		
		this._svgElement.setAttribute('width', groupBox.width + 'px');
		this._svgElement.setAttribute('height', groupBox.height + 'px');
		this._svgElement.style.marginLeft = groupBox.x + 'px';
		this._svgElement.style.marginTop = groupBox.y + 'px';
		
		if(!this._displayObject._stage) {
			// Opera fix -- Sometimes the box doesn't calculate before the removal, so we leave it in
			// there for a split second to ensure it calculates correctly.
			setTimeout(function(disp) { __covePool__.removeChild(disp); }, 1, this._displayObject);
		}
	},
	
	_appendPath: function(path) {
		if(this._currentFill || this._currentStroke) {
			if(this._currentFill) xb.Element.appendAttribute.call(this._currentFill, 'd', path);
			if(this._currentStroke) xb.Element.appendAttribute.call(this._currentStroke, 'd', path);
			this._redraw();
		}
	},
	
	
	//beginBitmapFill
	
	clear: function() {
		xb.Element.removeChildren.call(this._groupElement);
		this._currentFill = null;
		this._currentStroke = null;
	},
	
	beginFill: function(color, alpha) {
		var blue = color & 255;
		green = (color >> 8) & 255;
		red = (color >> 16) & 255,
		path = xb.SVG.create('path', {
			fill: 'rgb(' + red + ',' + green + ',' + blue + ')',
			fillOpacity: alpha
		});
		this._currentFill = path;
		this._groupElement.appendChild(path);
	},
	
	endFill: function() {
		this._currentFill = null;
	},
	
	drawRect: function(x, y, width, height) {
		this._appendPath([
			'M', x, y,
			'h', width,
			'v', height,
			'H', x,
			'Z'
		].join(' '));
	},
	
	drawRoundRect: function(x, y, width, height, ellipseWidth, ellipseHeight) {
		var w = width,
		h = height,
		rw = ellipseWidth > w ? w : ellipseWidth,
		rh = isNaN(ellipseHeight) ? ellipseWidth : ellipseHeight;
		
		if(rh > h) rh = h;
		rw /= 2;
		rh /= 2;
		
		var kw = xb.Math.roundTo(rw * xb.Math.KAPPA, 4),
		kh = xb.Math.roundTo(rh * xb.Math.KAPPA, 4);
		
		this._appendPath([
			'M', x + rw, y,
			'H', x + w - rw,
			'c', kw, '0', rw, rh - kh, rw, rh,
			'V', y + h - rh,
			'c', '0', kh, -rw + kw, rh, -rw, rh,
			'H', x + rw,
			'c', -kw, '0', -rw, -rh + kh, -rw, -rh,
			'V', y + rh,
			'c', '0', -kh, rw - kw, -rh, rw, -rh,
			'Z'
		].join(' '));
	},
	
	drawRoundRectComplex: function(x, y, width, height, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius) {
		// BEHAVIOUR IS YET TO BE MIRRORED
		
		var w = width,
		h = height,
		tlr = topLeftRadius,
		trr = topRightRadius,
		blr = bottomLeftRadius,
		brr = bottomRightRadius,
		tlk = xb.Math.roundTo(tlr * xb.Math.KAPPA, 4),
		trk = xb.Math.roundTo(trr * xb.Math.KAPPA, 4),
		blk = xb.Math.roundTo(blr * xb.Math.KAPPA, 4),
		brk = xb.Math.roundTo(brr * xb.Math.KAPPA, 4);
		
		this._appendPath([
			'M', x + tlr, y,
			'H', x + w - trr,
			'c', trk, '0', trr, trr - trk, trr, trr,
			'V', y + h - brr,
			'c', '0', brk, -brr + brk, brr, -brr, brr,
			'H', x + blr,
			'c', -blk, '0', -blr, -blr + blk, -blr, -blr,
			'V', y + tlr,
			'c', '0', -tlk, tlr - tlk, -tlr, tlr, -tlr,
			'Z'
		].join(' '));
	},
	
	drawCircle: function(x, y, radius) {
		/* Arcs do not work well with getBBox in browsers other than... IE9..? What world is this!?
		var path = (
			'M' + (x - radius) + ' ' + (y - radius) +
			'A 45 45 0 0 0 ' +
			(x + radius) + ' ' + (y + radius) +
			'A 45 45 0 0 0 ' +
			(x - radius) + ' ' + (y - radius) +
			'Z'
		); */
		
		var r = radius,
		k = xb.Math.roundTo(r * xb.Math.KAPPA, 4);
		
		this._appendPath([
			'M', x,     y - r,
			'C', x + k, y - r, x + r, y - k, x + r, y,
			'S', x + k, y + r, x,     y + r,
			'S', x - r, y + k, x - r, y,
			'S', x - k, y - r, x,     y - r,
			'Z'
		].join(' '));
	}
	
});